package com.ocom.common.exception.handle;


import com.ocom.common.exception.FileException;
import com.ocom.common.exception.ValidationException;
import com.ocom.common.resp.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindException;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@ControllerAdvice
public class GlobalExceptionHandler {

    private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler({RuntimeException.class})
    public ResponseEntity<Result> handleRunTimeException(RuntimeException e) {
        logger.error("", e);
        Result restError = new Result();
        restError.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
        restError.setMsg(e.getMessage());
        restError.setCode(500);
        ResponseEntity<Result> response = new ResponseEntity<>(restError, HttpStatus.BAD_REQUEST);
        return response;
    }

    @ExceptionHandler({Exception.class})
    public ResponseEntity<Result> handleException(Exception e) {
        logger.error("", e);
        Result restError = new Result();
        restError.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
        restError.setMsg(e.getMessage());
        restError.setCode(500);
        ResponseEntity<Result> response = new ResponseEntity<>(restError, HttpStatus.BAD_REQUEST);
        return response;
    }

    @ExceptionHandler({ValidationException.class})
    public ResponseEntity<Result> handleValidationException(ValidationException e, HttpServletRequest request) {
        logger.error(request.getRequestURL().toString() + "={}", e.getMessage());
        Result restError = new Result();
        Map<String,Object> map=new HashMap<>();
        map.put("path",request.getServletPath());
        map.put("badCode",HttpStatus.BAD_REQUEST.value());
        restError.setData(map);
        restError.setCode(e.getCode() != 0 ? e.getCode() : 400);
        restError.setMsg(e.getMessage());
        ResponseEntity<Result> response = new ResponseEntity<>(restError, HttpStatus.BAD_REQUEST);
        return response;
    }

    @ExceptionHandler(BindException.class)
    public ResponseEntity<Result> handleValidationExceptions(BindException ex) {
        List<ObjectError> errors = ex.getAllErrors();
        Result restError = new Result();
        Map<String,Object> map=new HashMap<>();
        restError.setCode(400);
        restError.setMsg(errors.get(0).getDefaultMessage());
        ResponseEntity<Result> response = new ResponseEntity<>(restError, HttpStatus.BAD_REQUEST);
        return response;
    }

    @ExceptionHandler({FileException.class})
    public ResponseEntity<Result> handleException(FileException e) {
        Result restError = new Result();
        restError.setCode(400);
        restError.setMsg(e.getMessage());
        ResponseEntity<Result> response = new ResponseEntity<>(restError, HttpStatus.BAD_REQUEST);
        return response;
    }


    @ExceptionHandler({NullPointerException.class})
    public ResponseEntity<Result> handleNullPointerException(NullPointerException e) {
        StackTraceElement ste =e.getStackTrace()[0];
        logger.error("空指针异常： 文件 【{}】   行数 【{}】  异常【{}】",ste.getFileName(),ste.getLineNumber(), e);
        Result restError = new Result();
        restError.setMsg("空指针异常");
        restError.setData("文件:"+ste.getFileName()+"行数:"+ste.getLineNumber()+"类名:"+ste.getClassName());
        restError.setCode(500);
        ResponseEntity<Result> response = new ResponseEntity<>(restError, HttpStatus.BAD_REQUEST);
        return response;
    }
}